<template>
    <div class="home-container">
        <div class="home-swiper-head">
            <i class="iconfont icon-tiantianquan"></i>
            <div class="swiper-name" ref="moduleRef">
                <span @click="changeSwiper(index)" v-for="(item, index) in moduleList"
                    :class="{ active: index === moduleIndex }">{{ item }}</span>
                <!-- 小红线 -->
                <div class="red-line" ref="redLineRef">
                    <div></div>
                </div>
            </div>
            <i class="iconfont icon-search"></i>
        </div>
        <swiper @swiper="onSwiper" @slideChange="onSlideChange" :initialSlide="moduleIndex" @progress="onProgress"
            class="my-swiper">
            <swiper-slide>
                <div class="silde-content">1</div>
            </swiper-slide>
            <swiper-slide>
                <!-- <div class="silde-content">2</div> -->
                <Explore></Explore>
            </swiper-slide>
            <swiper-slide>
                <div class="silde-content">3</div>
            </swiper-slide>
        </swiper>
    </div>
</template>

<script setup>
import { ref, reactive, onMounted, nextTick } from 'vue'
import { Swiper, SwiperSlide } from 'swiper/vue'
import 'swiper/css'
import Explore from '@/views/Home/Explore.vue'

let moduleList = reactive(['关注', '发现', '附件'])

//点击切换标题
let moduleIndex = ref(1)
const changeSwiper = (index) => {
    moduleIndex.value = index
    mySwiper?.slideTo(index)
}

//初始化swiper
let mySwiper = null;
const onSwiper = (swiper) => {
    mySwiper = swiper
}
const onSlideChange = ({ activeIndex }) => {
    //activeIndex 滑动对应的索引值
    moduleIndex.value = activeIndex
}

//红线-跟随选中的module平移和缩放
const redLineRef = ref(null)
const moduleRef = ref(null)

//计算每个关键位置时，红线的偏移量
let posList = [] // 用于存储红线在每个标题正下方时距离窗口左侧距离的数组
const getMoudleParams = () => {
    const modules = moduleRef.value.querySelectorAll('span')
    //获取红线宽度
    const redLineWidth = redLineRef.value.getBoundingClientRect().width
    //遍历每个标题，计算其到窗口左侧的距离
    for (let i = 0; i < modules.length; i++) {
        let moduleRect = modules[i].getBoundingClientRect()
        //红线在每个标题正下方时距离窗口左侧的距离 = 当前标题距左侧的距离 + 当前标题宽度的一半 - 红线宽度的一半
        posList.push(moduleRect.left + moduleRect.width / 2 - redLineWidth / 2);
    }
}

//监听滑动过程，使用progress计算
const onProgress = ({ progress }) => {
    //进程不管Swiper有几页都是重0-1划分
    if (posList.length === 0) return
    //滑动到最后一个模块，且滑动完毕
    if (progress === 1) {
        redLineRef.value.style.transform = `translateX(${posList[posList.length - 1]}px)`;
        redLineRef.value.children[0].style.transform = `scaleX(1)`;
        return
    }
    //计算红线处于那个标题之间
    //计算偏移量
    let nowPosIdx = progress * (moduleList.length - 1);
    let nowPosIdxInt = Math.floor(nowPosIdx);
    //计算当前progress下的偏移量
    let trans = (posList[nowPosIdxInt + 1] - posList[nowPosIdxInt]) * (nowPosIdx - nowPosIdxInt) + posList[nowPosIdxInt];

    //计算滑动过程中的缩放
    let maxScale = 2 //最大缩放
    let gap = nowPosIdx - nowPosIdxInt
    let scale = gap <= 0.5 ? gap : 1 - gap // 先拉伸后还原
    scale = scale * maxScale + 1

    redLineRef.value.style.transform = `translateX(${trans}px)`;
    redLineRef.value.children[0].style.transform = `scaleX(${scale})`;

}

onMounted(() => {
    getMoudleParams()
    onProgress({ progress: moduleIndex.value == 0 ? 0 : moduleIndex.value / (moduleList.length - 1) })
    // 监听页面尺寸更改红线位置
    window.addEventListener('resize',()=>{
        nextTick(()=>{
            posList = []
            getMoudleParams()
        })
    })
})
</script>

<style lang="scss" scoped>
.home-container {

    .my-swiper {
        height: 100vh;
        background-color: rgb(249, 250, 249);

        .silde-content {
            @include flexcc;
            height: 50vh;
        }
    }

    .home-swiper-head {
        position: fixed;
        top: 0;
        left: 0;
        height: 3rem;
        right: 0;
        background-color: #fff;
        padding: 0 1rem;
        z-index: 2;
        @include flexbc;

        .iconfont {
            font-size: 1.2rem;
            color: rgb(51, 51, 51);
        }

        .iconfont:nth-child(1) {
            color: rgb(248, 230, 28)
        }

        .swiper-name {
            font-size: 0.9rem;
            // @include flexcc;
            color: rgb(149, 149, 149);

            span {
                padding: .9375rem;
                box-sizing: content-box;
                transition: all .1s linear;

                &.active {
                    font-size: 1rem;
                    color: rgb(51, 51, 51);
                    font-weight: bold;
                }
            }

            //小红线样式
            .red-line {
                position: absolute;
                bottom: 0.5rem;
                left: 0;
                transition: all .2s linear;

                >div {
                    border-radius: .1875rem;
                    background-color: #ff0000;
                    transition: all .2s linear;
                    width: 1.875rem;
                    height: .1875rem;
                }
            }
        }

    }
}
</style>